//
//  MemberSubViewController.m
//  Rail
//
//  Created by hychen on 2018/3/5.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "MemberSubViewController.h"
#import "MemberStatisticTableViewCell.h"
#import "MemberInfoViewController.h"
@interface MemberSubViewController ()

@end

@implementation MemberSubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshData {
    self.tableView.mj_footer = nil;
    self.currentPage = 1;
    self.isOpenFooter = NO;
    BMWeakSelf
    [HttpRequest loadFactoryUserListWithAreaId:self.areaId type:[NSString stringWithFormat:@"%ld",(long)self.type] pageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
            weakSelf.dataArray = [NSMutableArray arrayWithArray:object];
            if (object.count > 9) {
                weakSelf.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:weakSelf refreshingAction:@selector(loadMoreData)];
            }
        }
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_footer resetNoMoreData];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (void)loadMoreData {
    self.currentPage++;
    BMWeakSelf
    [HttpRequest loadFactoryUserListWithAreaId:self.areaId type:[NSString stringWithFormat:@"%ld",self.type] pageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
            [weakSelf.dataArray addObjectsFromArray:object];
            if (object.count < 10) {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            [weakSelf.tableView reloadData];
        }
        else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WorkAreaUserModel *model = [self.dataArray objectAtIndex:indexPath.row];
    MemberStatisticTableViewCell *cell = [MemberStatisticTableViewCell cellWithTableView:tableView class:[MemberStatisticTableViewCell class]];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WorkAreaUserModel *model = [self.dataArray objectAtIndex:indexPath.row];
    model.areaid = self.areaId;
    MemberInfoViewController *vc = [MemberInfoViewController new];
    BMWeakSelf
    [vc setReturnDeviceSuccessBlock:^{
        [weakSelf refreshData];
    }];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
